//
//  UserCompanyTheMain.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/9/6.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
protocol UserCompanyTheMainDelegate{
    func comple(theMainArr: NSArray) -> Void
}
class UserCompanyTheMain: UIView {
    @IBOutlet weak var customView: UIView!
    @IBOutlet weak var mr: UIButton!
    @IBOutlet weak var ys: UIButton!
    @IBOutlet weak var wx: UIButton!
    @IBOutlet weak var ss: UIButton!
    @IBOutlet weak var zx: UIButton!
    @IBOutlet weak var pxxx: UIButton!
    @IBOutlet weak var zxgl: UIButton!
    @IBOutlet weak var yq: UIButton!
    
    var delegate: UserCompanyTheMainDelegate?
    
    var theMainArr: [String]! = []// 选了哪些主营
    
    var maxHeight: CGFloat = SCREEN_HEIGHT - 400
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }
    
    
    func commonInit () {
        NSBundle.mainBundle().loadNibNamed("UserCompanyTheMain", owner: self, options: nil)
        self.customView.frame.size.width = self.frame.size.width
        self.customView.width(self.width)
        self.customView.height(300)
        self.customView.bottom(self.bottom())
        self.backgroundColor = UIColor.clearColor()
        self.alpha = 0
        
        
        let maskView:UIView = UIView.init(frame: self.bounds)
        maskView.backgroundColor = UIColor.blackColor()
        maskView.alpha = 0.5
        self.addSubview(maskView)
        
        self.addSubview(self.customView)
        let tap = UITapGestureRecognizer.init(target: self, action:#selector(UserCompanyTheMain.tapClick(_:)))
        maskView.addGestureRecognizer(tap)
        self.viewFrame()
    }

//    override func drawRect(rect: CGRect) {
//        self.mr.layer.masksToBounds = true
//        self.mr.layer.borderWidth = 1
//        self.mr.layer.borderColor = BODER_COLOR.CGColor
//        
//        self.ys.layer.masksToBounds = true
//        self.ys.layer.borderWidth = 1
//        self.ys.layer.borderColor = BODER_COLOR.CGColor
//        
//        self.wx.layer.masksToBounds = true
//        self.wx.layer.borderWidth = 1
//        self.wx.layer.borderColor = BODER_COLOR.CGColor
//        
//        self.ss.layer.masksToBounds = true
//        self.ss.layer.borderWidth = 1
//        self.ss.layer.borderColor = BODER_COLOR.CGColor
//        
//        self.zx.layer.masksToBounds = true
//        self.zx.layer.borderWidth = 1
//        self.zx.layer.borderColor = BODER_COLOR.CGColor
//        
//        self.pxxx.layer.masksToBounds = true
//        self.pxxx.layer.borderWidth = 1
//        self.pxxx.layer.borderColor = BODER_COLOR.CGColor
//        
//        self.zxgl.layer.masksToBounds = true
//        self.zxgl.layer.borderWidth = 1
//        self.zxgl.layer.borderColor = BODER_COLOR.CGColor
//        
//        self.yq.layer.masksToBounds = true
//        self.yq.layer.borderWidth = 1
//        self.yq.layer.borderColor = BODER_COLOR.CGColor
//    }
    //MARK:view的位置
    func viewFrame() {
        
        if maxHeight == kScreen_height {
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.customView.frame.origin.y = kScreen_height
                self.alpha = 0
            })
        } else {
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.alpha = 1
                self.customView.bottom(kScreen_height)
            })
        }
    }

    //主营按钮点击事件
    @IBAction func theMainClicked(sender: UIButton){
        if sender == self.mr{
            let btn: UIButton = sender
            btn.selected = !btn.selected
            if btn.selected{
                self.mr.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.mr.titleLabel?.text)!)
                
            }else{
                self.mr.backgroundColor = UIColor.whiteColor()
                
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.mr.titleLabel?.text)!)!)
            }
        }
        
        if sender == self.ys{
            sender.selected = !sender.selected
            if sender.selected{
                self.ys.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.ys.titleLabel?.text)!)
            }else{
                self.ys.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.ys.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.wx{
            sender.selected = !sender.selected
            if sender.selected{
                self.wx.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.wx.titleLabel?.text)!)
            }else{
                self.wx.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.wx.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.ss{
            sender.selected = !sender.selected
            if sender.selected{
                self.ss.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.ss.titleLabel?.text)!)
            }else{
                self.ss.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.ss.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.zx{
            sender.selected = !sender.selected
            if sender.selected{
                self.zx.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.zx.titleLabel?.text)!)
            }else{
                self.zx.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.zx.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.pxxx{
            sender.selected = !sender.selected
            if sender.selected{
                self.pxxx.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.pxxx.titleLabel?.text)!)
            }else{
                self.pxxx.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.pxxx.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.zxgl{
            sender.selected = !sender.selected
            if sender.selected{
                self.zxgl.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.zxgl.titleLabel?.text)!)
            }else{
                self.zxgl.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.zxgl.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.yq{
            sender.selected = !sender.selected
            if sender.selected{
                self.yq.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.yq.titleLabel?.text)!
                )

            }else{
                self.yq.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.yq.titleLabel?.text)!)!)
            }
        }
    }
    
    func tapClick (sender:UITapGestureRecognizer) {
        self.cancelBtnClicked(nil)
    }
    
    @IBAction func cancelBtnClicked(sender: UIButton?){
        self.maxHeight = kScreen_height
        self.viewFrame()
        
    }
    
    @IBAction func yesBtnClicked(sender: UIButton){
        delegate!.comple(theMainArr)
        
         self.cancelBtnClicked(nil)
    }

    
}
